package com.qfedu.zf.life.controller;


import cn.hutool.core.convert.NumberWithFormat;
import com.qfedu.zf.common.config.SystemConfig;
import com.qfedu.zf.common.param.CommentCreatDTO;
import com.qfedu.zf.common.param.CommentDTO;
import com.qfedu.zf.common.utils.JwtUtil;
import com.qfedu.zf.common.vo.R;

import com.qfedu.zf.life.entity.PostPage;
import com.qfedu.zf.life.mapper.CommentMapper;
import com.qfedu.zf.life.mapper.PostPageMapper;
import com.qfedu.zf.life.service.CommentService;
import com.qfedu.zf.life.service.ImageService;
import com.qfedu.zf.life.service.PostPageService;
import com.qfedu.zf.life.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wjw
 * @since 2024-06-20
 */
@RestController
@RequestMapping("/life/postPage")
public class PostPageController {
    @Autowired
    private PostPageService postPageService;

    @Autowired
    private PostPageMapper postPageMapper;
    @Autowired
    private ImageService imageService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private UserService userService;


    @Autowired
    private CommentMapper commentMapper;

    @GetMapping("/all")
    public List<com.qfedu.zf.common.param.PostPage> getAllPosts() {

        return postPageMapper.getAllPosts();
    }

//    @GetMapping("/{postId}")
//    public Map<String, Object> getPostById(@PathVariable("postId") Long postId) {
//        PostPage postPage = postPageService.getPostById(postId);
//        User user = userService.getUserById(postPage.getUserId());
//        List<Image> images = imageService.getImagesByPostId(postId);
//        List<Comment> comments = commentService.getCommentsByPostId(postId);
//
//        Map<String, Object> response = new HashMap<>();
//        response.put("postPage", postPage);
//        response.put("user", user);
//        response.put("images", images);
//        response.put("comments", comments);
//
//        return response;
//    }

    @GetMapping("/{postPageId}")
    public List<com.qfedu.zf.common.param.PostDetails> selectPostsDetailById(@PathVariable("postPageId") Long postPageId) {

        return postPageMapper.selectPostsDetailById(postPageId);
    }

    @PostMapping("/{postPageId}/like")
    public void likePost(@PathVariable("postPageId") Long postPageId) {
        PostPage postPage = postPageService.getPostById(postPageId);
        if (postPage != null) {
            postPage.setLikeCount(postPage.getLikeCount() + 1);
            postPageService.updateById(postPage);
        }
    }

    @GetMapping("/{postPageId}/commentList")
    public R getAllComment(@PathVariable("postPageId") Long postPageId) {
        List<CommentDTO> commentDTOList = commentMapper.selectCommentDTOList(postPageId);
        return R.ok(commentDTOList);
    }


    @PostMapping("/{postPageId}/addComment")
    public R addComment(HttpServletRequest request,@PathVariable("postPageId") Long postPageId, @RequestBody CommentCreatDTO commentCreatDTO) {

        // 1.判断请求参数
        if (commentCreatDTO == null) {
            return R.fail("参数错误");
        }

        // 配置userid
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        Long id = (long) ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();

        commentCreatDTO.setUserId(id);
        commentCreatDTO.setPostPageId(postPageId);


        try {
            commentService.addComment(commentCreatDTO);
            return R.ok("发表评论成功!");
        } catch (Exception e) {
            return R.fail("发表评论失败: " + e.getMessage());
        }

    }

}

